import {
  openWheel,
  postReset
} from '../../api/index'
const app = getApp();
Page({
  data: {
    show: {
      show1: false,
      show2: true,
      show3: true,
      show4: true,
      show5: true,
      show6: true,
      show7: true,
      show8: true,
      show9: true,
      show10: true,
    },
    number: 0,

  },
  async openWheel(e) {
    const number = Number(e.currentTarget.dataset.index);
    console.log(number);
    wx.setStorage({
      key: "number",
      data:number
    })
    const res = await openWheel(number)
    console.log(res);
    const isBooleanArray = res.data.rows.map(item => item.isBoolean);
    console.log(isBooleanArray);
    const i = 0
    if (isBooleanArray[0] === "true") {
      this.setData({
        "show.show1": true,
      });
    } else {
      this.setData({
        "show.show1": false,
      });
    }
    if (isBooleanArray[1] === "true") {
      this.setData({
        "show.show2": true,
      });
    } else {
      this.setData({
        "show.show2": false,
      });
    }
    if (isBooleanArray[2] === "true") {
      this.setData({
        "show.show3": true,
      });
    } else {
      this.setData({
        "show.show3": false,
      });
    }
    if (isBooleanArray[3] === "true") {
      this.setData({
        "show.show4": true,
      });
    } else {
      this.setData({
        "show.show4": false,
      });
    }
    if (isBooleanArray[4] === "true") {
      this.setData({
        "show.show5": true,
      });
    } else {
      this.setData({
        "show.show5": false,
      });
    }
    if (isBooleanArray[5] === "true") {
      this.setData({
        "show.show6": true,
      });
    } else {
      this.setData({
        "show.show6": false,
      });
    }
    if (isBooleanArray[6] === "true") {
      this.setData({
        "show.show7": true,
      });
    } else {
      this.setData({
        "show.show7": false,
      });
    }
    if (isBooleanArray[7] === "true") {
      this.setData({
        "show.show8": true,
      });
    } else {
      this.setData({
        "show.show8": false,
      });
    }
    if (isBooleanArray[8] === "true") {
      this.setData({
        "show.show9": true,
      });
    } else {
      this.setData({
        "show.show9": false,
      });
    }
    if (isBooleanArray[9] === "true") {
      this.setData({
        "show.show10": true,
      });
    } else {
      this.setData({
        "show.show10": false,
      });
    }

    console.log(this.data.show);
  },
  async openWheel2(number) {
    console.log(number);
    const res = await openWheel(number)
    console.log(res);
    const isBooleanArray = res.data.rows.map(item => item.isBoolean);
    console.log(isBooleanArray);
    if (isBooleanArray[0] === "true") {
      this.setData({
        "show.show1": true,
      });
    } else {
      this.setData({
        "show.show1": false,
      });
    }
    if (isBooleanArray[1] === "true") {
      this.setData({
        "show.show2": true,
      });
    } else {
      this.setData({
        "show.show2": false,
      });
    }
    if (isBooleanArray[2] === "true") {
      this.setData({
        "show.show3": true,
      });
    } else {
      this.setData({
        "show.show3": false,
      });
    }
    if (isBooleanArray[3] === "true") {
      this.setData({
        "show.show4": true,
      });
    } else {
      this.setData({
        "show.show4": false,
      });
    }
    if (isBooleanArray[4] === "true") {
      this.setData({
        "show.show5": true,
      });
    } else {
      this.setData({
        "show.show5": false,
      });
    }
    if (isBooleanArray[5] === "true") {
      this.setData({
        "show.show6": true,
      });
    } else {
      this.setData({
        "show.show6": false,
      });
    }
    if (isBooleanArray[6] === "true") {
      this.setData({
        "show.show7": true,
      });
    } else {
      this.setData({
        "show.show7": false,
      });
    }
    if (isBooleanArray[7] === "true") {
      this.setData({
        "show.show8": true,
      });
    } else {
      this.setData({
        "show.show8": false,
      });
    }
    if (isBooleanArray[8] === "true") {
      this.setData({
        "show.show9": true,
      });
    } else {
      this.setData({
        "show.show9": false,
      });
    }
    if (isBooleanArray[9] === "true") {
      this.setData({
        "show.show10": true,
      });
    } else {
      this.setData({
        "show.show10": false,
      });
    }
    console.log(this.data.show);
  },
  async postReset() {
    wx.showModal({
      title: '提示',
      content: '重置后中奖记录将清空，是否重置',
      success: async (res) =>  {
        if (res.confirm) {
          console.log('用户点击确定')
          const res = await postReset()
          console.log(res);
          if (res.data.code === 200) {
            wx.setStorage({
              key: "number",
              data:0
            })
            wx.showToast({
              title: '重置成功',
              icon: 'success'
            });
             // 重置成功后重新加载当前页面
                this.setData({
                  number: 0,
                });
            this.openWheel2(this.data.number)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },

  onLoad(options) {
      app.bgPause()
    wx.getStorage({
      key: 'number',
      success: (res) => {
        console.log(res,'number');
        this.setData({
          number: res.data.number,
        });
      }
    })
    this.openWheel2(this.data.number)
  },
})